﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Interfaces;
using DataAccess;
using System.ComponentModel;
using BusinessObjects;
using DCMSExceptions;
using CryptoUtils;
using Common;
using System.Data;

namespace BusinessLogic {
  /// <summary>
  /// Business Logic Layer for users
  /// </summary>
  public class UserBll : IUserLogic {

    private UserDal dal = new UserDal( );

    /// <summary>
    /// Gets the user.
    /// </summary>
    /// <param name="loginName">Name of the login.</param>
    /// <returns></returns>
    public User GetUser( string loginName ) {
      User user;
      try {
        user = dal.GetUser( loginName );
      } catch ( DataException ex ) {
        throw new BusinessException( "Fout bij ophalen van gebruiker.", ex );
      }
      return user;
    }

    /// <summary>
    /// Saves the user.
    /// </summary>
    /// <param name="user">The user.</param>
    public void SaveUser( BusinessObjects.User user ) {
      try {
        dal.SaveUser( user );
      } catch ( DataException ex ) {
        throw new BusinessException( ex.Message, ex );
      }
    }

    /// <summary>
    /// Gets the auto logon user.
    /// </summary>
    /// <returns></returns>
    public User GetAutoLogonUser( ) {
      User user;
      try {
        user = dal.GetAutoLogonUser( );
      } catch ( DataException ex ) {
        throw new BusinessException( "Fout bij automatische login van gebruiker.", ex );
      }
      return user;
    }

    /// <summary>
    /// Saves the user password.
    /// </summary>
    /// <param name="user">The user.</param>
    public void SaveUserPassword( User user ) {
      try {
        dal.SaveUserPassword( user );
      } catch ( DataException ex ) {
        throw new BusinessException( ex.Message, ex );
      }
    }

    /// <summary>
    /// Authenticates the current user.
    /// </summary>
    /// <param name="loginName">Name of the login.</param>
    /// <param name="givenPassword">The given password.</param>
    /// <returns></returns>
    public bool AuthenticateCurrentUser( string loginName, string givenPassword ) {

      string hash = Cryptography.CreatePasswordHash( SettingsSingleton.Settings.CurrentUser.Salt, givenPassword );
      return ( SettingsSingleton.Settings.CurrentUser.Password == hash );
    }

    /// <summary>
    /// Authenticates the user.
    /// </summary>
    /// <param name="loginName">Name of the login.</param>
    /// <param name="givenPassword">The given password.</param>
    /// <returns></returns>
    public bool AuthenticateUser( string loginName, string givenPassword ) {
      User user = dal.GetUser( loginName );
      string hash = Cryptography.CreatePasswordHash( user.Salt, givenPassword );
      if ( user.Password != hash ) {
        user = null;
        return false;
      } else {
        SettingsSingleton.Settings.CurrentUser = user;
        return true;
      }
    }


  }
}
